package com.supreme.nettyapp.generalclient.pingclient.controller;

import com.supreme.nettyapp.generalclient.pingclient.service.PingClientService;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttQoS;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "ping")
public class PIngClientController {

    @Resource
    private PingClientService pingClientService;

    @GetMapping(value = "ping")
    public String ping() {
        MqttFixedHeader fixedHeader = new MqttFixedHeader(
                MqttMessageType.PINGREQ,
                false,
                MqttQoS.AT_MOST_ONCE,
                false,
                0);
        MqttMessage mqttMessage = new MqttMessage(fixedHeader);
        pingClientService.writeAndFlush(mqttMessage);
        return "success";
    }
}
